package com.njym.java.security.app.rest;


import com.njym.java.security.app.biz.UserGroupsToUserBiz;
import com.njym.java.security.app.entity.FamilyUserGroups;
import com.njym.java.security.app.entity.UserGroupsToUser;
import com.njym.java.security.app.entity.UserGroupsToUserVo;
import com.njym.java.security.common.msg.ObjectRestResponse;
import com.njym.java.security.common.rest.BaseController;
import com.njym.java.security.common.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("userGroupsToUser")
public class UserGroupsToUserController extends BaseController<UserGroupsToUserBiz,UserGroupsToUser> {
    /**
     * 查询会客厅
     * @param userId
     * @return
     */
    @RequestMapping(value = "/queryGroup",method = RequestMethod.GET)
    @ResponseBody
    public Object queryGroup(String userId){
        if (!userId.equals("undefined")) {
            Integer isFamily = 0;
            List<UserGroupsToUserVo> list = baseBiz.queryGroup(Integer.parseInt(userId), isFamily);
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<UserGroupsToUserVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }


    }

    /**
     * 添加群
     * @param userId
     * @return
     */
    @RequestMapping(value = "/addGroup",method = RequestMethod.GET)
    @ResponseBody
    public Object addGroup(String userId, String username, String friendIds,String names){
        if (!userId.equals("undefined")) {
            String[] fri = friendIds.split(",");
            String[] na = names.split(",");
            FamilyUserGroups familyUserGroups = new FamilyUserGroups();

            familyUserGroups.setUgName(username+","+ StringUtils.join(names, ","));
            familyUserGroups.setUgCreateTime(new Date());
            familyUserGroups.setUsAdminId(Integer.parseInt(userId));
            familyUserGroups.setUgNum(fri.length + 1);
            baseBiz.addFamilyGroup(familyUserGroups);
            int groupId = familyUserGroups.getId();
            for (int i = 0; i < fri.length; i++){
                UserGroupsToUser userGroupsToUser = new UserGroupsToUser();
                userGroupsToUser.setUserId(Integer.parseInt(fri[i]));
                userGroupsToUser.setGroupId(groupId);
                userGroupsToUser.setCreateTime(new Date());
                userGroupsToUser.setGroupNick(na[i]);
                baseBiz.insertSelective(userGroupsToUser);
            }
            UserGroupsToUser user = new UserGroupsToUser();
            user.setUserId(Integer.parseInt(userId));
            user.setGroupId(groupId);
            user.setCreateTime(new Date());
            user.setGroupNick(username);
            baseBiz.insertSelective(user);


            Map<String, Object> data = new HashMap<>(1);
            data.put("group", familyUserGroups);
            return new ObjectRestResponse<UserGroupsToUserVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }


    }

    /**
     * 查询群成员信息
     * @param groupId
     * @return
     */
    @RequestMapping(value = "/queryGroupUser",method = RequestMethod.GET)
    @ResponseBody
    public Object queryGroupUser(String groupId){
        if (!groupId.equals("undefined")) {
            List<UserGroupsToUserVo> list = baseBiz.queryGroupUser(Integer.parseInt(groupId));
            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<UserGroupsToUserVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }

    /**
     * 查询家庭群
     * @param userId
     * @return
     */
    @RequestMapping(value = "/queryFamilyGroup",method = RequestMethod.GET)
    @ResponseBody
    public Object queryFamilyGroup(String userId){
        if (!userId.equals("undefined")) {
            Integer isFamily = 1;
            List<UserGroupsToUserVo> list = baseBiz.queryGroup(Integer.parseInt(userId), isFamily);

            Map<String, Object> data = new HashMap<>(1);
            data.put("list", list);
            return new ObjectRestResponse<UserGroupsToUserVo>().rel(true).data(data);
        } else {
            return ResponseUtil.fail(40301, "错误！");
        }

    }
}